<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */
class BackMenuService extends ServiceBase {


    public function getList(){
        //允许排序的字段名称数组
        $canOrderCols = array('name', 'keyword');

        $id = Input::get( 'id', 0 );
        $order = Input::get( 'order' );
        $reverse = Input::get( 'reverse',0 );
        $page = Input::get( 'page', 1 );

        $name = str_replace(' ', '%', Input::get( 'name', '' ));
        $keyword = Input::get( 'keyword', null );

        $entries = Menu::orderby(in_array($order, $canOrderCols)?$order:'name', $reverse == 0?'desc':'asc')->where('parent_id',$id);

        if(!empty($name)){
            $entries = $entries->where('name', 'like', '%'.$name.'%');
        }

        if(!empty($keyword)){
            $entries = $entries->where('keyword', 'like', '%'.$keyword.'%');
        }

        $entries = $entries->paginate(Config::get('app.pagenate_num'))
            ->addQuery('order',$order)
            ->addQuery('reverse',$reverse)

            ->addQuery('name',$name)
            ->addQuery('keyword',$keyword);

        $links = array(
            "order"=>$order,
            "reverse"=>$reverse,
            "page"=>$page,
            'name' => $name,
            'keyword' => $keyword,
        );

        return array(
            'entries'=>$entries,
            'links'=>$links,
        );

    }

    public function createMenu(){
        $menu = new Menu();
        $this->parseInput($menu,'name');
        $this->parseInput($menu,'keyword');
        $this->parseInput($menu,'parent_id',0);

        if(!$menu->save()){
            $this->errors()->merge($menu->errors());
            return false;
        }

        return true;
    }

    public function updateMenu($menu){

        $this->parseInput($menu,'name');
        $this->parseInput($menu,'keyword');
        $this->parseInput($menu,'parent_id');

        if(!$menu->save()){
            $this->errors()->merge($menu->errors());
            return false;
        }

        return true;
    }

    public function deleteMenu($menu){
        if(!$menu->delete()){
            $this->errors()->merge($menu->errors());
            return false;
        }
        return true;
    }

    public function exportMenu(){
        $result = $this->getList();
        $table = $result['entries'];
        $type = Input::get( 'type' );

        $columnTitle = Menu::$column;

        switch($type){
            case 'csv':
                $output='';

                $output .=  implode(",",$columnTitle)."\r\n";

                $output .= Menu::getCSVContent($table);

                $headers = array(
                    'Content-Type' => "text/csv;charset='utf-8'",
                    'Content-Disposition' => 'attachment; filename="menu_'.date('Y_m_d_His').'.csv"',
                );

                return array('output'=>rtrim($output, "\r\n"), 'headers'=>$headers);
            case 'excel':

                $output = '<table><thead><tr>';
                foreach($columnTitle as $columnName){
                    $output = $output.'<th>'.$columnName.'</th>';
                }
                $output = $output.'</tr></thead>';

                $output = $output.Menu::getExcelContent($table);

                $output = $output.'<tfoot><tr><td colspan="'.count($columnTitle).'"></td></tr></tfoot></table>';

                $headers = array(
                    'Pragma' => 'public',
                    'Expires' => 'public',
                    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
                    'Cache-Control' => 'private',
                    'Content-Type' => "application/vnd.ms-excel;;charset='utf-8'",
                    'Content-Disposition' => 'attachment; filename=menu_'.date('Y_m_d_His').'.xls',
                    'Content-Transfer-Encoding' => ' binary'
                );
                return array('output'=>$output, 'headers'=>$headers);
        }
        $this->errors()->add('error', '导出失败');
        return false;
    }


}

